#ifndef __STRINGPROTOCOL_H__
#define __STRINGPROTOCOL_H__

#include "communication/commdef.h"
#include "lib/stringex.h"


#define STRING_PROTOCOL_LINE_MAX_LEN 128
#define STRING_PROTOCOL_ETX  '\n'

//string line 消息基类
class StringMessage : public IProtocolMessage
{
public:
    explicit StringMessage(){}
    virtual ~StringMessage(){}

public:
    std::string Cmd;
    std::string ArgsStr;
    //解析后的参数，一般是接收参数
    std::vector<std::string> Args;
    //原始str
    std::string RawStr;

public:
    //创建发送消息
    static std::shared_ptr<StringMessage> CreateMessage(const std::string& cmd, const std::string& argsStr);
    //创建接收消息
    static std::shared_ptr<StringMessage> CreateMessage(const std::string& cmd, const std::vector<std::string>& args);
    std::string ToString();
};

/// @brief 
class StringProtocol : public IProtocol
{
public:
    explicit StringProtocol();
    virtual ~StringProtocol();

public:
    int Packet(std::shared_ptr<IProtocolMessage> msg);
    uint8_t *GetSendBuffer();
    std::shared_ptr<IProtocolMessage> Parse(uint8_t b);
    void Reset();

    bool IsAck(std::shared_ptr<IProtocolMessage> sendMsg, std::shared_ptr<IProtocolMessage> recvMsg);
    bool IsResponse(std::shared_ptr<IProtocolMessage> sendMsg, std::shared_ptr<IProtocolMessage> recvMsg);

private:
    std::shared_ptr<IProtocolMessage> ParseHandle();
    void DumpErrorMessage();

private:
    uint8_t m_sendLine[STRING_PROTOCOL_LINE_MAX_LEN];
    std::string m_recvLine;
};

#endif // __STRINGPROTOCOL_H__
